using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TTSoft.MyWeb.Models.Repositories
{
	#region using
	using TTSoft.MyWeb.Helpers;
	using TTSoft.MyWeb.Models.ViewModels;
	#endregion
	public class ThuocRepository
	{
		public List<Thuoc> SelectAll()
		{
			return DataContext.Instance.Thuocs.ToList();
		}

		public Thuoc SelectByID(int id)
		{
			return DataContext.Instance.Thuocs.FirstOrDefault(item => item.ID == id );
		}

		public List<Thuoc> SelectPaging(int page, int pageSize)
		{
			var lstItem = DataContext.Instance.Thuocs.ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

		public bool Insert(Thuoc obj)
		{
			try
			{
				DataContext.Instance.Thuocs.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.Thuocs.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.Thuocs.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public int GetTotalItem()
		{
			return DataContext.Instance.Thuocs.Count();
		}

		public int GetTotalItemByIDLoaiThuoc(int idloaithuoc)
		{
			return (from p in DataContext.Instance.Thuocs where p.IDLoaiThuoc == idloaithuoc select p).Count();
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.NumThuocPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByIDLoaiThuoc(int idloaithuoc)
		{
			int RecordCount = GetTotalItemByIDLoaiThuoc(idloaithuoc);
			int PageSize = WebConfiguration.NumThuocPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}


		public List<Thuoc> RetrieveByID(int id)
		{
			return (from item in DataContext.Instance.Thuocs where  item.ID == id  select item).ToList();
		}

		public List<Thuoc> SelectByIDLoaiThuoc(int idloaithuoc, int page, int pageSize)
		{
			var lstItem = (from item in DataContext.Instance.Thuocs where item.IDLoaiThuoc == idloaithuoc select item).ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

        public List<Thuoc> SearchThuoc(string tenthuoc, string nuocsanxuat)
        {
            var lstThuoc = DataContext.Instance.Thuocs.ToList();
            List<Thuoc> lstItem = new List<Thuoc>();
            for (int i = 0; i < lstThuoc.Count; i++)
            {
                if (lstThuoc[i].TenThuoc.ToUpper().Contains(tenthuoc == "" ? lstThuoc[i].TenThuoc.ToUpper() : tenthuoc.ToUpper()) &&
                            lstThuoc[i].NuocSanXuat.ToUpper().Contains(nuocsanxuat == "" ? lstThuoc[i].NuocSanXuat.ToUpper() : nuocsanxuat.ToUpper()))
                {
                    lstItem.Add(lstThuoc[i]);
                }
            }
            return lstItem;
        }
	}
}